package game

import (
	// "server/GoUtil"
	// "server/MergeConst"
	"server/game/internal"

	"server/pkg/github.com/name5566/leaf/gate"
)

// import "reflect"

//"fmt"

type Player struct {
	playerdata   map[string]PlayerDataModule
	playerdataIF map[string]interface{}
	M_DwUin      int32
	PAMgr        *PlayerActiveMgr
}

func (p *Player) InitPlayer(UserName string) {
	p.playerdata = make(map[string]PlayerDataModule)
	p.playerdataIF = make(map[string]interface{})
	Base := &PlayerBaseData{PlayerData: NewPlayerData("PlayerBaseData", p)}
	p.playerdata["PlayerBaseData"] = Base
	p.playerdataIF["PlayerBaseData"] = Base
	ok := Base.LoadDataFromDB(UserName)
	if ok {
		p.playerdata["PlayerBaseData"] = Base
		p.playerdataIF["PlayerBaseData"] = Base
	}
	p.M_DwUin = Base.Data.DwUin

	unlock := &PlayerEmitUnlockData{PlayerData: NewPlayerData("PlayerEmitUnlockData", p)}
	ok4 := unlock.LoadDataFromDB(Base.Data.DwUin)
	if ok4 {
		p.playerdata["PlayerEmitUnlockData"] = unlock
		p.playerdataIF["PlayerEmitUnlockData"] = unlock
	}

	Detail := &PlayerEmitDetaliData{PlayerData: NewPlayerData("PlayerEmitDetaliData", p)}
	ok2 := Detail.LoadDataFromDB(Base.Data.DwUin)
	if ok2 {
		p.playerdata["PlayerEmitDetaliData"] = Detail
		p.playerdataIF["PlayerEmitDetaliData"] = Detail
		G_getGameLogic().Mlogger.Debug("load PlayerEmitDetaliData:", Base.Data.DwUin)
	}

	Chess := &PlayerChessData{PlayerData: NewPlayerData("PlayerChessData", p)}
	ok3 := Chess.LoadDataFromDB(Base.Data.DwUin)
	if ok3 {
		p.playerdata["PlayerChessData"] = Chess
		p.playerdataIF["PlayerChessData"] = Chess
	}

	Chest := &PlayerUnlockingChestData{PlayerData: NewPlayerData("PlayerUnlockingChestData", p)}
	ok1 := Chest.LoadDataFromDB(Base.Data.DwUin)
	if ok1 {
		p.playerdata["PlayerUnlockingChestData"] = Chest
		p.playerdataIF["PlayerUnlockingChestData"] = Chest
	}

	Single := &PlayerSingleData{PlayerData: NewPlayerData("PlayerSingleData", p)}
	ok6 := Single.LoadDataFromDB(Base.Data.DwUin)
	if ok6 {
		p.playerdata["PlayerSingleData"] = Single
		p.playerdataIF["PlayerSingleData"] = Single
	}

	Charge := &PlayerChargeData{PlayerData: NewPlayerData("PlayerChargeData", p)}
	ok7 := Charge.LoadDataFromDB(Base.Data.DwUin)
	if ok7 {
		p.playerdata["PlayerChargeData"] = Charge
		p.playerdataIF["PlayerChargeData"] = Charge
	}

	friendEvt := &PlayerFriendEventData{PlayerData: NewPlayerData("PlayerFriendEventData", p)}
	ok8 := friendEvt.LoadDataFromDB(Base.Data.DwUin)
	if ok8 {
		p.playerdata["PlayerFriendEventData"] = friendEvt
		p.playerdataIF["PlayerFriendEventData"] = friendEvt
	}

	p.PAMgr = new(PlayerActiveMgr)
	p.PAMgr.InitActiveMgr(p)
}

func (p *Player) ClearData() {

	for _, v := range p.playerdata {
		v.ClearData()
	}

}

func (p *Player) AutoSaveData() {

	for _, v := range p.playerdata {
		v.SaveDataFromDB("")
	}

}
func (p *Player) Reconnect() {
	for _, v := range p.playerdata {
		v.Reconnect(true)
	}
}

func (p *Player) GetIFGameData(key string) interface{} {

	v, ok := p.playerdataIF[key]
	if ok {
		return v
	}
	return nil
}

func (p *Player) GetGameData(key string) PlayerDataModule {

	v, ok := p.playerdata[key]
	if ok {
		return v
	}
	return nil
}

func (p *Player) GetAgentByPlayer() gate.Agent {
	for k, v := range internal.Agents {
		if v.(*Player) == p {
			return k
		}
	}
	return nil
}

// func (p *Player) CreatePlayerDataMudule(Name string ){
// 	PlayerData
// }
